package com.johnson.news.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import io.reactivex.functions.Consumer;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.johnson.news.R;
import com.johnson.news.activity.MyAskListActivity;
import com.johnson.news.databinding.FragmentPostComplaintBinding;
import com.johnson.news.event_msg.PostComplaintMsg;
import com.johnson.news.model.bean.BaseResponseData;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseBindingFragment;
import com.youmbe.library.util.MyToast;

import org.greenrobot.eventbus.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class PostComplaintFragment extends BaseBindingFragment<FragmentPostComplaintBinding> {

    public PostComplaintFragment() {
        // Required empty public constructor
    }

    InputMethodManager inputMethodManager;
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(rootView == null) {
            rootView = super.onCreateView(inflater, container, savedInstanceState);
            inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        }

        return rootView;
    }

    @Override
    public void init() {
        dataBinding.askComplaintTitle.setVisibility(View.GONE);
        dataBinding.commitBtn.setOnClickListener(onClickListener);
    }

    void submit() {
        String title = dataBinding.titleEdit.getText().toString();
        if(TextUtils.isEmpty(title)) {
            MyToast.showToast(getString(R.string.ask_title_empty), getActivity());
            return;
        }
        String content = dataBinding.contentEdit.getText().toString();
        if(TextUtils.isEmpty(content)) {
            MyToast.showToast(getString(R.string.ask_content_empty), getActivity());
            return;
        }
        String name = dataBinding.nameEdit.getText().toString();
        if(TextUtils.isEmpty(name)) {
            MyToast.showToast(getString(R.string.ask_name_empty), getActivity());
            return;
        }
        String email = dataBinding.emailEdit.getText().toString();
        if(TextUtils.isEmpty(email)) {
            MyToast.showToast(getString(R.string.ask_email_empty), getActivity());
            return;
        }
        if(!Utils.isEmail(email)) {
            MyToast.showToast(getString(R.string.ask_email_invalid), getActivity());
            return;
        }

        String phone = dataBinding.phoneEdit.getText().toString();
        if(TextUtils.isEmpty(phone)) {
            MyToast.showToast(getString(R.string.ask_phone_empty), getActivity());
            return;
        }

        if(!Utils.verifyPhone(phone)) {
            MyToast.showToast(getString(R.string.login_invalid_phone), getActivity());
            return;
        }

//        String address = dataBinding.addressEdit.getText().toString();
//        if(TextUtils.isEmpty(address)) {
//            MyToast.showToast(getString(R.string.ask_address_empty), getActivity());
//            return;
//        }
//
//        String department = dataBinding.departmentEdit.getText().toString();
////        if(TextUtils.isEmpty(department)) {
////            MyToast.showToast(getString(R.string.ask_department_empty), getActivity());
////            return;
////        }

        RemoteDataSource.getRemoteDataSource()
                .postComplaint(getActivity(), title, content, name, email, phone)//, address, department
                .subscribe(new Consumer<BaseResponseData>() {
                    @Override
                    public void accept(BaseResponseData statusItem) throws Exception {
                        MyToast.showToast(statusItem.msg, getActivity());
                        inputMethodManager.hideSoftInputFromWindow(dataBinding.departmentEdit.getWindowToken(), 0);
                        if(statusItem.status == BaseResponseData.response_ok) {
//                            Intent intent = new Intent(getActivity(), MyAskListActivity.class);
//                            startActivity(intent);
                            clearInput();
                            EventBus.getDefault().post(new PostComplaintMsg());
//                            MyToast.showToast(getString(R.string.ask_commit_success), getActivity());
                        }
//                        else
//                            MyToast.showToast(getString(R.string.ask_commit_fail), getActivity());
                    }
                });

    }

    void clearInput() {
        dataBinding.titleEdit.setText("");
        dataBinding.contentEdit.setText("");
        dataBinding.emailEdit.setText("");
        dataBinding.phoneEdit.setText("");
        dataBinding.addressEdit.setText("");
        dataBinding.departmentEdit.setText("");
        dataBinding.nameEdit.setText("");
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.commit_btn) {
                submit();
            }
        }
    };

    @Override
    public int getContentViewId() {
        return R.layout.fragment_post_complaint;
    }
}
